
package com.aiwei.tdjk.utils;


import com.aiwei.tdjk.entity.User;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelParseToList {

	public static final String houseInfoSourceFilePath = "/Users/mac/Documents/工作/天地健康城/sys_user-new.xlsx";

	public static List<User> parseExcel() throws Exception {

		InputStream is = new FileInputStream(new File(houseInfoSourceFilePath));
		XSSFWorkbook houseInfoWBook = new XSSFWorkbook(is);
		List<User> userList = new ArrayList<>();
		User user = null;

//		for (int i = 0; i < houseInfoWBook.getNumberOfSheets(); i++) {
		for (int i = 0; i < 1; i++) {
			XSSFSheet sheet = houseInfoWBook.getSheetAt(i);
			if (sheet == null) {
				continue;
			}
			for (int index = 1; index <= sheet.getLastRowNum(); index++) {
				XSSFRow row = sheet.getRow(index);
				if (row != null) {
					user = new User();
					

//					if (!row.getCell(0).toString().trim().equals("")) {
//
//						if(row.getCell(0).getCellType()==0){
//							DecimalFormat df = new DecimalFormat("0");
//							String whatYourWant = df.format(row.getCell(0).getNumericCellValue());
//							//System.out.println(+ row.getCell(0).getCellType() + "whatYourWant" + row.getCell(0).toString().trim());
//							user.setUsername(whatYourWant);
//						}else {
//							user.setUsername(row.getCell(0).getStringCellValue().trim());
//						}
//					}

					if (!row.getCell(1).toString().trim().equals("")) {
						user.setFullName(row.getCell(1).getStringCellValue().trim());
					}
					if (!row.getCell(4).toString().trim().equals("")) {
						user.setEmployeeId(row.getCell(4).getStringCellValue().trim());
					}
					if (!row.getCell(3).toString().trim().equals("")) {
						user.setUsername(row.getCell(3).getStringCellValue().trim());
					}

					userList.add(user);
				}

			}
		}

		return userList;

	}

	public static void main(String args[]) throws Exception {
		for (User user : ExcelParseToList.parseExcel()) {
			System.out.println(user.getUsername());
		}
	}

}

